/**
 * @author Steve Sedlmayr
 * @mail ssedlmayr@gmail.com
 * @link http://code.google.com/p/chitin/
 * @version 0.1
 
	The MIT License

	Copyright (c) 2009 Steve Sedlmayr

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

package srs.chitin.mvstate.base
{
	import flash.utils.*;
	
	/** 
	 * A base class for a generic VO. This class is coded in such a way
	 * that subclasses can initialize via the contructor to an arbitrary set of fields.
	 */
	public dynamic class ModelBaseVO implements IModelVO {
		/** 
		 * A String enumeration of all of this VO's fields.
		 */
		[ArrayElementType("String")]
		protected var _enumeration:Array = new Array();
		
		/** 
		 * An enumeration object used to initialize the VO with the desired fields.
		 */
		protected var _enumerationObject:Object = new Object();
		
		/**
		 * The ModelBaseVO constructor. Calls <code>setEnumeration</code> on the subclass.
		 */ 
		public function ModelBaseVO() : void {
			getDefinitionByName(getQualifiedClassName(this)).setEnumeration(this);
		}
		
		/** 
		 * Return a field's value by its name. Fields are stored permanently 
		 * as class members (statics) on the VO's class.
		 * 
		 * @param parameterName A string specifying which property to return.
		 */
		public function getPropertyByName(parameterName:String) : * {
			var propertyString:String = "_" + parameterName;
			
			if ( getDefinitionByName(getQualifiedClassName(this))[propertyString] != null ) {
				return getDefinitionByName(getQualifiedClassName(this))[propertyString];
			}
			
			return null;
		}
		
		/** 
		 * Set the value of a field on the VO, using it's name as a key.  Will only set the value
		 * if the class definition of the desired value and the property match.
		 * 
		 * @param parameterName The name of the field you want to change.
		 * @param value The value to which to set the field specified in parameterName.
		 */
		public function setPropertyByName(parameterName:String, value:*) : void {
			var propertyString:String = "_" + parameterName;
			var valueDefinition:Object = getDefinitionByName(getQualifiedClassName(value));
			
			if ( getDefinitionByName(getQualifiedClassName(this))[propertyString] != null ) {
				var propertyDefinition:Object = getDefinitionByName(getQualifiedClassName(getDefinitionByName(getQualifiedClassName(this))[propertyString]));
				var definitionsAreNumbers:Boolean = Boolean( ( valueDefinition == int || valueDefinition == Number || valueDefinition == uint ) && 
					( propertyDefinition == int || propertyDefinition == Number || propertyDefinition == uint ) ); //make an exception for raw number types
				
				//If the class definition of the desired value and the property match, set the value
				if ( propertyDefinition == valueDefinition || definitionsAreNumbers == true ) {
					getDefinitionByName(getQualifiedClassName(this))[propertyString] = value;
				} else {
					throw new Error("TYPE ERROR: the data-types of the property you are setting and the value you are setting it to do not match. " + 
						"The property will not be updated.");
				}
			}
		}
		
		/**
		 * Adds a property to <code>_enumerationObject</code> in the case when
		 * the enumeration object is not initialiazed in the constructor. This
		 * is useful when cloning one VO from a source VO.
		 * 
		 * @param parameterName The name of the field you want to change.
		 * @param value The value to which to set the field specified in parameterName.
		 */
		public function addEnumerationItem(parameterName:String, value:*) : void {
			if ( this._enumerationObject[parameterName] == null ) { this._enumerationObject[parameterName] = value; }
			
			getDefinitionByName(getQualifiedClassName(this)).setEnumeration(this);
		}
		
		/** 
	 	 * Get the entire list of VO field names to iterate over it and perform some operation
	 	 * via <code>getPropertyByName()</code>.
		 * 
		 * @return Returns an array of field names.
		 */
		public function getEnumeration() : Array {
			return this._enumeration;
		}
		
		/** 
		 * Initialize the current VO class with fields and values.
		 * Called only once from the constructor.
		 */
		protected static function setEnumeration(currentObject:ModelBaseVO) : void {
			if ( currentObject._enumeration != null && currentObject._enumerationObject != null ) {
				//Populate the enumeration set from the enumeration object; only populate undefined properties
				for ( var property:String in currentObject._enumerationObject ) {
					var hasProperty:Boolean = false;
					var propertyString:String = "_" + property;
					
					if ( getDefinitionByName(getQualifiedClassName(currentObject))[propertyString] == null ) {
						getDefinitionByName(getQualifiedClassName(currentObject))[propertyString] = currentObject._enumerationObject[property];
					}
					
					for ( var i:Number = 0; i < currentObject._enumeration.length; i++ ) {
						if ( currentObject._enumeration[i] == property ) {
							hasProperty = true;
							break;
						}
					}
					
					if ( hasProperty == false ) { currentObject._enumeration.push(property); }
				}
			}
		}
	}
}